package com.oocl.aeat.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oocl.aeat.dao.MerchantDao;
import com.oocl.aeat.pojo.Merchant;
import com.oocl.aeat.util.DBHelper;
import com.oocl.aeat.util.DataSourceUtils;

public class MerchantDaoImpl implements MerchantDao {
	
	@Override
	public int addMerchant(Merchant merchant) {
		String sql = "insert into m_user(id, name, store_name, pwd, idcard,img,address,state,block) values(?,?,?,?,?,?,?,?,?)";
		Connection conn = DataSourceUtils.getConn();
		PreparedStatement pst = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, merchant.getId());
			pst.setString(2, merchant.getName());
			pst.setString(3, merchant.getStoreName());
			pst.setString(4, merchant.getPwd());
			pst.setString(5, merchant.getIdcard());
			pst.setString(6, merchant.getImg());
			pst.setString(7, merchant.getAddress());
			pst.setInt(8, merchant.getState());
			pst.setInt(9, merchant.getBlock());
			return pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(pst, null, null);
		}
		return -1;
	}

	@Override
	public int deleteMerchant(String id) {
		String sql = "delete from m_user where id = ? ";
		Connection conn = DataSourceUtils.getConn();
		PreparedStatement pst = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, id);
			return pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(pst, null, null);
		}
		return -1;
	}

	@Override
	public int updateMerchant(Merchant merchant) {
		String sql = "update m_user set name=?, store_name=?, pwd=?, idcard=?,img=?,address=?,state=?,block=? where id = ? ";
		Connection conn = DataSourceUtils.getConn();
		PreparedStatement pst = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, merchant.getName());
			pst.setString(2, merchant.getStoreName());
			pst.setString(3, merchant.getPwd());
			pst.setString(4, merchant.getIdcard());
			pst.setString(5, merchant.getImg());
			pst.setString(6, merchant.getAddress());
			pst.setInt(7, merchant.getState());
			pst.setInt(8, merchant.getBlock());
			pst.setString(9, merchant.getId());
			return pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(pst, null, null);
		}
		return -1;
	}

	@Override
	public List<Merchant> queryAllMerchant(Integer block) {
		Connection conn = DataSourceUtils.getConn();
		List<Merchant> list = new ArrayList<Merchant>();
		Merchant merchant = null;
		String sql = "select * from m_user where block = ? ";
		PreparedStatement pst = null;
		ResultSet rst = null;
		try {
			pst =  conn.prepareStatement(sql);
			pst.setInt(1, block);
			rst = pst.executeQuery();
		
			while(rst.next()){
				merchant = new Merchant();
				merchant.setId(rst.getString(1));
				merchant.setName(rst.getString(2));
				merchant.setStoreName(rst.getString(3));
				merchant.setPwd(rst.getString(4));
				merchant.setIdcard(rst.getString(5));
				merchant.setImg(rst.getString(6));
				merchant.setAddress(rst.getString(7));
				merchant.setState(rst.getInt(8));
				merchant.setBlock(rst.getInt(9));
				list.add(merchant);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(pst, rst, null);
		}
		return list;
	}

	@Override
	public Merchant queryMerchantById(String id) {
		Connection conn = DataSourceUtils.getConn();
		Merchant merchant = null;
		String sql = "select * from m_user where id = ? ";
		PreparedStatement pst = null;
		ResultSet rst = null;
		try {
			pst =  conn.prepareStatement(sql);
			pst.setString(1, id);
			rst = pst.executeQuery();
		
			while(rst.next()){
				merchant = new Merchant();
				merchant.setId(rst.getString(1));
				merchant.setName(rst.getString(2));
				merchant.setStoreName(rst.getString(3));
				merchant.setPwd(rst.getString(4));
				merchant.setIdcard(rst.getString(5));
				merchant.setImg(rst.getString(6));
				merchant.setAddress(rst.getString(7));
				merchant.setState(rst.getInt(8));
				merchant.setBlock(rst.getInt(9));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(pst, rst, null);
		}
		return merchant;
	}
}
